package com.sicheng.lc.周赛.分类.模拟;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/22 20:32
 */
public class 按奇偶性交换后的最大数字 {
    //https://leetcode.cn/problems/largest-number-after-digit-swaps-by-parity/
    public int largestInteger(int num) {
        //1234
        char[] nums = String.valueOf(num).toCharArray();

        for (int i = 0; i < nums.length - 1; i++) {
            int max = nums[i] - '0';
            int k = -1;
            for (int j = i + 1; j < nums.length; j++) {
                if (((nums[j] - '0') & 1) == (((nums[i] - '0') & 1))) {
                    if (nums[j] - '0' > max) {
                        k = j;
                        max = nums[j] - '0';
                    }
                }
            }
            if (k != -1) {
                swap(nums, k, i);
            }
        }
        int x = 0;
        for (int j : nums) {
            x = x * 10 + j - '0';
        }
        return x;
    }

    private void swap(char[] nums, int k, int i) {
        char t = nums[k];
        nums[k] = nums[i];
        nums[i] = t;
    }

    public static void main(String[] args) {
        按奇偶性交换后的最大数字 solution = new 按奇偶性交换后的最大数字();
        solution.largestInteger(60);
    }
}
